package com.itc.bi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itc.bi.constant.SystemConstant;
import com.itc.bi.entity.DatasourceEntity;
import com.itc.bi.entity.PythonTemplateEntity;
import com.itc.bi.mapper.PythonTemplateMapper;
import com.itc.bi.service.DatasourceService;
import com.itc.bi.service.PythonTemplateIService;
import com.itc.common.core.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @description 针对表【python_template_table(python模板表)】的数据库操作service实现
 * @author zcw
 * @createTime 2024-6-19 14:50
 */
@Service
@Slf4j
public class PythonTemplateServiceImpl extends ServiceImpl<PythonTemplateMapper, PythonTemplateEntity> implements PythonTemplateIService {

    @Autowired
    DatasourceService datasourceService;

    /**
     * 新增python模板信息
     */
    @Override
    public void savePythonTemplate(PythonTemplateEntity entity) {
        PythonTemplateEntity old = getOne(Wrappers.lambdaQuery(PythonTemplateEntity.class).eq(PythonTemplateEntity::getQueryName, entity.getQueryName()));
        if(null != old && old.getId().equals(entity.getId())){
            throw new ServiceException("名称已存在");
        }
        saveOrUpdate(entity);
    }

    /**
     * 根据python模板id获取python模板信息
     * @param id 主键id
     * @return python模板信息
     */
    @Override
    public PythonTemplateEntity get(Long id) {
        // 根据id从数据库中查询python模板
        PythonTemplateEntity pythonTemplate = this.getById(id);
        // 判断python模板是否存在
        if(Objects.isNull(pythonTemplate)){
            throw new ServiceException("该id的python模板不存在");
        }
        return pythonTemplate;
    }

    /**
     * 查询所有的python模板列表
     * @return 所有的python模板列表
     */
    @Override
    public List<PythonTemplateEntity> listPythonTemplate() {
        return list();
    }

    /**
     * 根据python模板id删除python模板信息
     * @param id 主键id
     * @return true or false删除是否成功
     */
    @Override
    public Boolean removePythonTemplate(Long id) {
        // 判断该id的python模板是否还存在
        if(Objects.isNull(this.getById(id))){
            throw new ServiceException("该id的python模板已不存在！");
        }
        // 存在才可进行删除操作
        return removeById(id);
    }

    @Override
    public String initContent(Long id,Long dsId){

        PythonTemplateEntity template = getById(id);
        if(null == template){
            throw new ServiceException("记录不存在");
        }
        String type = template.getType();

        DatasourceEntity datasource = datasourceService.getById(dsId);
        if(null == datasource){
            throw new ServiceException("数据源不存在");
        }

        if(!datasource.getType().equals(type)){
            throw new ServiceException(String.format("数据源类型:%s,模板类型:%s",datasource.getType(),template.getType()));
        }
        //文本
        String content = template.getQueryContent();
        String userName = datasource.getUserName();
        String password = datasource.getPassword();

        content = content.replace("${userName}",userName);
        content = content.replace("${password}",password);

        String jdbcUrl = datasource.getJdbcUrl();

        int endIndex = jdbcUrl.length();
        if(jdbcUrl.contains("?")){
            endIndex = jdbcUrl.indexOf("?");
        }
        String startSplit = "";

        if(SystemConstant.DATASOURCE_TYPE_ORACLE.equals(type)){
            startSplit = "@";
        }else{
            startSplit = "//";
        }
        jdbcUrl = jdbcUrl.substring(jdbcUrl.indexOf(startSplit)+startSplit.length(),endIndex);
        jdbcUrl = jdbcUrl.replace("/",":");
        String[] hostAndPortAndDb = jdbcUrl.split(":");
        content = content.replace("${host}",hostAndPortAndDb[0]);
        content = content.replace("${port}",hostAndPortAndDb[1]);
        content = content.replace("${db}",hostAndPortAndDb[2]);
        if(SystemConstant.DATASOURCE_TYPE_PG.equals(type)){
            String schema = datasourceService.schema(datasource);
            content = content.replace("${schema}",schema);
        }
        return content;
    }
}
